//
//  PicKerCollectionView.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/18.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit

fileprivate let picPickerCell = "picPickerCell"
fileprivate let edgeMargin : CGFloat = 10


class PicKerCollectionView: UICollectionView {
    
    // MARK:- 定义属性
    var images : [UIImage] = [UIImage](){
        didSet{
            reloadData()
        }
    }

    override func awakeFromNib() {
        
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemWH = (UIScreen.main.bounds.size.width - 4 * edgeMargin) / 3
        layout.itemSize = CGSize.init(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = edgeMargin
        layout.minimumInteritemSpacing = edgeMargin
        
        contentInset = UIEdgeInsets.init(top: edgeMargin, left: edgeMargin, bottom: 0, right: edgeMargin)
        
        register(UINib.init(nibName: "PicPickerCell", bundle: nil), forCellWithReuseIdentifier: picPickerCell)
        dataSource = self
    }
 

}

extension PicKerCollectionView : UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return images.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picPickerCell, for: indexPath) as! PicPickerCell
        
        cell.image = indexPath.item <= images.count - 1 ? images[indexPath.item] : nil
        
        return cell
    }
}



